package com.xydtech.ecard.merchant.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.ecard.merchant.domain.EcardPriorCredit;
import com.xydtech.ecard.merchant.service.IEcardPriorCreditService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 电子名片预授信记录Controller
 *
 * @author yuzhiguo
 * @date 2024-05-21 下午2:57
 */
@Controller
@RequestMapping("/ecard/priorCredit")
public class EcardPriorCreditRecordController extends BaseController
{
    private String prefix = "merchant";

    @Autowired
    private IEcardPriorCreditService ecardPriorCreditService;

    @RequiresPermissions("ecard:prior:view")
    @GetMapping()
    public String priorCredit()
    {
        return prefix + "/priorCredit";
    }

    /**
     * 查询电子名片预授信记录列表
     */
    @RequiresPermissions("ecard:prior:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EcardPriorCredit ecardPriorCredit)
    {
        String salt = getSalt();//盐值
        startPage();
        List<EcardPriorCredit> list = ecardPriorCreditService.selectEcardPriorCreditList(ecardPriorCredit, salt);
        return getDataTable(list);
    }

    /**
     * 导出电子名片预授信记录列表
     */
    @RequiresPermissions("ecard:prior:export")
    @Log(title = "电子名片预授信记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EcardPriorCredit ecardPriorCredit)
    {
        String salt = getSalt();//盐值
        List<EcardPriorCredit> list = ecardPriorCreditService.selectEcardPriorCreditList(ecardPriorCredit, salt);
        ExcelUtil<EcardPriorCredit> util = new ExcelUtil<EcardPriorCredit>(EcardPriorCredit.class);
        return util.exportExcel(list, "电子名片预授信记录数据");
    }

    /**
     * 选择转交的客户经理
     */
    @RequiresPermissions("ecard:prior:handOver")
    @GetMapping("/priorHandOver/{ids}")
    public String priorHandOver(@PathVariable("ids") String ids, ModelMap mmap) {
        mmap.put("ids", ids);
        return prefix + "/priorChoiceUser";
    }

    /**
     * 预授信转交
     */
    @Log(title = "预授信转交客户经理", businessType = BusinessType.UPDATE)
    @PostMapping("/priorAllocation")
    @ResponseBody
    public AjaxResult priorAllocation(Long userId, Long deptId, Long[] ids) {
        if(userId == null){
            return AjaxResult.error("请选择移交人员！");
        }
        if(deptId == null){
            return AjaxResult.error("请选择移交的机构！");
        }

        //移交预授信列表
        List<EcardPriorCredit> ecardPriorCreditList = new ArrayList<>();

        for (int i = 0; i < ids.length; i++) {
            //修改客户经理id和机构id
            EcardPriorCredit ecardPriorCredit = new EcardPriorCredit();
            ecardPriorCredit.setId(ids[i]);
            ecardPriorCredit.setUserId(userId);
            ecardPriorCredit.setDeptId(deptId);
            ecardPriorCreditList.add(ecardPriorCredit);
        }

        return toAjax(ecardPriorCreditService.priorAllocation(ecardPriorCreditList, userId));
    }

    /**
     * 新增电子名片预授信记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存电子名片预授信记录
     */
    @RequiresPermissions("ecard:prior:add")
    @Log(title = "电子名片预授信记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EcardPriorCredit ecardPriorCredit)
    {
        String salt = getSalt();//盐值
        return toAjax(ecardPriorCreditService.insertEcardPriorCredit(ecardPriorCredit, salt));
    }

    /**
     * 修改电子名片预授信记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        String salt = getSalt();//盐值
        EcardPriorCredit ecardPriorCredit = ecardPriorCreditService.selectEcardPriorCreditById(id, salt);
        mmap.put("ecardPriorCredit", ecardPriorCredit);
        return prefix + "/edit";
    }

    /**
     * 修改保存电子名片预授信记录
     */
    @RequiresPermissions("ecard:prior:edit")
    @Log(title = "电子名片预授信记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EcardPriorCredit ecardPriorCredit)
    {
        String salt = getSalt();//盐值
        return toAjax(ecardPriorCreditService.updateEcardPriorCredit(ecardPriorCredit, salt));
    }

    /**
     * 删除电子名片预授信记录
     */
    @RequiresPermissions("ecard:prior:remove")
    @Log(title = "电子名片预授信记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ecardPriorCreditService.deleteEcardPriorCreditByIds(ids));
    }
}
